<?php

namespace catchAdmin\phone\model;

use catchAdmin\model\Notice;
use catcher\exceptions\AlertErrorException;

class ZhaoPin extends \catchAdmin\model\ZhaoPin
{

    /**
     * 保存
     */
    public function saveData($param)
    {
        $this->startTrans();

        $msg = $this->saveDataApp($param);

        // 2.发消息
        (new Notice())->saveDataCC([
            // 个人认证——待审核
            'notice_type'    => 5,
            'title'          => '待审核',
            'content'        => "等待后台审核",
            'wechat_user_id' => $msg['wechat_user_id'],
        ]);

        $this->commit();

        return 1;
    }


    /**
     * 停止招聘
     */
    public function stop($param)
    {
        $msg = $this
            ->where([
                'id'             => $param['id'],
                'wechat_user_id' => $this->getLoginId(),
            ])
            ->find();
        if (empty($msg)) {
            throw new AlertErrorException();
        }

        $msg->status = 2;
        $msg->save();

        return 1;
    }

    ######################################## 显示 ################################################


    /**
     * 列表
     */
    public function index($param)
    {
        $app = $this
            ->with(['wechatUser']);

        if (!empty($param['type'])) {
            if ($param['type'] == 'self') {
                $app = $app->where('wechat_user_id', $this->getLoginId());
            } elseif ($param['type'] == 'other') {
                $app = $app
                    ->where('audit_status', 2)
                    ->where('show_end_date', ">=", date('Y-m-d'))
                    ->order('audit_time desc');
            }
        }

        return $this->indexDataCC($param, $app);
    }


    /**
     * 详情
     */
    public function read($param)
    {
        $msg = $this
            ->where('id', $param['id'])
            ->with(['wechatUser'])
            ->find();

        $msg['bao_is'] = 0;
        if ((new \catchAdmin\model\ZhaoPinBao())
            ->where([
                'zhao_pin_id'    => $msg['id'],
                'wechat_user_id' => $this->getLoginId(),
            ])
            ->count()) {
            $msg['bao_is'] = 1;
        }

        return $msg;
    }
}
